home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10288 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.7 KB  |  187 lines

  1. Path: lantana.singnet.com.sg!usenet
  2. From: s8700055@singnet.com.sg (XY Xie)
  3. Newsgroups: comp.lang.c
  4. Subject: Novice programmer needs help!
  5. Date: Sat, 16 Mar 1996 17:10:53 GMT
  6. Organization: Singapore Telecom Internet Service
  7. Message-ID: <4ieov7$r5@lantana.singnet.com.sg>
  8. NNTP-Posting-Host: ts900-2822.singnet.com.sg
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11.     I'm learning C programming myself from a book and I recently wrote
  12. this programme and I could not figure out why the errors occured.
  13. Could some kind soul out there check the code for me? I used upper
  14. case for all the error messages and I included them on a new line
  15. following  the supposedly erroneous code (just to separate them from
  16. the usual programming comments).
  17.  
  18. #include <stdio.h>
  19. #include <conio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #define totnum 20       //total no. of entries in list=20
  24.  
  25. struct list{
  26.  char name[15];         //alloc 15 chars for name
  27.  char sex;
  28.  int age;
  29. };
  30.  
  31. void displaymenu(void);           //display menu for choice
  32. char getchoice(void);                        //get a choice from  user
  33. void addlist(struct list *friend[totnum]);         //add items to list
  34. void allocatemem(struct list *friend[totnum]);     //allocate memory 
  35. void displist(struct list *friend[totnum]);        //display list  
  36. void search(struct list *friend[totnum]);          //search a name
  37.  void dealloc(struct list *friend[totnum]);         //deallocate mem
  38. // THE ABOVE 5 LINES GOT "ERROR: ) EXPECTED"
  39.  
  40. int num=0;              //current no. of entries
  41.  
  42. main()
  43. {
  44.  char choice;
  45.  struct list *friend[numtot];
  46.  //"ERROR:DECLARATION TERMINATED INCORRECTLY IN FUNCTION MAIN()"
  47.  
  48.   do
  49.  {displaymenu();
  50.   choice=getchoice();
  51.  
  52.   switch(choice)
  53.   {case('a'):{addlist(friend);
  54.                   break;}
  55.     case('b'):{displist(friend);
  56.                   break;}
  57.     case('c'):{search(friend);
  58.                   break;}
  59.     case('d'):{dealloc(friend);
  60.                   exit(1);
  61.                   break;}
  62.     //ALL THE 4 FUNCTIONS I CALLED ABOVE HAVE THESE:
  63.     //"ERROR : CALL TO UNDEFINED FUNCTION IN FUNCTION MAIN()" and
  64.     //"ERROR : EXPRESSION SYNTAX IN FUNCTION MAIN()"
  65.  
  66.     default:{puts("Wrong entry. Try again");
  67.                 break;}
  68.   }
  69.  } while(choice!='d');
  70.  return 0;
  71. }
  72. //*****************************************************
  73. void displaymenu(void)             //display menu
  74. {
  75.  puts("\n\t\tFRIENDS' PARTICULARS LIST");
  76.  puts("\t\t-------------------------\n");
  77.  puts("a. Add to list.");
  78.  puts("b. Display list.");
  79.  puts("c. Get person.");
  80.  puts("d. Exit program.");
  81.  printf("Enter your choice.(a,b,c or d):");
  82.  return;
  83. }
  84. //*****************************************************
  85. char getchoice(void)               //input choice
  86. {
  87.  char ans;
  88.  fflush(stdin);
  89.  ans=getche();
  90.  ans=tolower(ans);     //convert all input to lower case
  91.  printf("\n");
  92.  return ans;
  93. }
  94. //*****************************************************
  95. void addlist(struct list *friend[])        //add to list
  96. //  "ERROR: ) EXPECTED"
  97.  
  98. {
  99.  if(num==totnum)
  100.  {puts("List is full");
  101.   return;}
  102.  allocatemem(friend);           //alloc mem for 1 structure variable
  103.  printf("\nEnter name. ");
  104.  fflush(stdin);
  105.  fgets(friend[num]->name[],15,stdin);
  106.  printf("Enter sex. ");
  107.  fflush(stdin);
  108.  friend[num]->sex=getchar();
  109.  printf("Enter age. ");
  110.  scanf(" %d", &friend[num]->age);
  111.  num++;
  112.  return;
  113. }
  114. //***************************************************
  115. void allocatemem(struct list *friend[])         //alloc heap mem
  116. {
  117.  friend[num]=(struct list*)malloc(sizeof(struct list));
  118.  if(!friend[num])
  119.  {puts("Memory error,");
  120.   exit(1);}
  121.  return;
  122. }
  123. //*****************************************************
  124. void displist(struct list *friend[])        //display entries and
  125. {                                           //calc average age
  126.  int c;
  127.  float avg=friend[0]->age;
  128.  if(num==0)
  129.  {puts("\n\nThere is no name on the list");
  130.   return;}
  131.  for(c=0;c<num;c++)
  132.  {
  133.   printf("\n #%d\n",c+1);
  134.   printf(" name:%ssex:%c\nage:%d\n",friend[c]->name[],
  135.             friend[c]->sex,friend[c]->age);
  136.   if(c!=0)
  137.   {avg+=friend[c]->age;}
  138.  }
  139.     avg/=(float)c;
  140.     printf("\n Average age is %.1f.\n",avg);
  141.     return;
  142. }
  143. //******************************************************************
  144. void search(struct list *friend[])
  145. {
  146.  int c,match=0;
  147.  char ss[15];
  148.  printf("\nEnter name to search. ");
  149.  fflush(stdin);
  150.  fgets(ss,15,stdin);
  151.  for(c=0;c<num;c++)
  152.  {ss[c]=tolower(ss[c]);}         //convert the user's input to lower
  153. case
  154.  for(c=0;c<num;c++)
  155.  {if(!strcmp(ss,friend[c]->name[]));
  156.   {printf("\n #%d\n",c+1);
  157.     printf(" name:%s sex:%c\n age:%d\n",friend[c]->name[],friend[c]->sex,
  158.              friend[c]->age);
  159.     match=1;}
  160.   }
  161.   if(match==0)
  162.   puts(" No match found.");
  163.  
  164.  return;
  165. }
  166. //********************************************************************
  167. void dealloc(struct list *friend[])
  168. {
  169.  int c;
  170.  for(c=0;c<num;c++)
  171.  {
  172.   free(friend[c]);
  173.  }
  174.  return;
  175. }
  176.  
  177.  
  178.     That's all . THANX!
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.